/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.awt.BorderLayout;
/*   4:    */ import java.awt.GridLayout;
/*   5:    */ import java.awt.Image;
/*   6:    */ import java.io.IOException;
/*   7:    */ import java.util.ArrayList;
/*   8:    */ import java.util.List;
/*   9:    */ import javax.imageio.ImageIO;
/*  10:    */ import javax.swing.ImageIcon;
/*  11:    */ import javax.swing.JLabel;
/*  12:    */ import javax.swing.JPanel;
/*  13:    */ import javax.swing.JScrollPane;
/*  14:    */ import javax.swing.JTable;
/*  15:    */ import javax.swing.JViewport;
/*  16:    */ import javax.swing.table.AbstractTableModel;
/*  17:    */ import javax.swing.table.TableModel;
/*  18:    */ import nl.siegmann.epublib.browsersupport.NavigationEvent;
/*  19:    */ import nl.siegmann.epublib.browsersupport.NavigationEventListener;
/*  20:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  21:    */ import nl.siegmann.epublib.domain.Book;
/*  22:    */ import nl.siegmann.epublib.domain.Metadata;
/*  23:    */ import nl.siegmann.epublib.domain.Resource;
/*  24:    */ import org.apache.commons.lang.StringUtils;
/*  25:    */ import org.slf4j.Logger;
/*  26:    */ import org.slf4j.LoggerFactory;
/*  27:    */ 
/*  28:    */ public class MetadataPane
/*  29:    */   extends JPanel
/*  30:    */   implements NavigationEventListener
/*  31:    */ {
/*  32: 32 */   private static final Logger log = LoggerFactory.getLogger(MetadataPane.class);
/*  33:    */   private static final long serialVersionUID = -2810193923996466948L;
/*  34:    */   private JScrollPane scrollPane;
/*  35:    */   
/*  36:    */   public MetadataPane(Navigator navigator)
/*  37:    */   {
/*  38: 38 */     super(new GridLayout(1, 0));
/*  39: 39 */     this.scrollPane = ((JScrollPane)add(new JScrollPane()));
/*  40: 40 */     navigator.addNavigationEventListener(this);
/*  41: 41 */     initBook(navigator.getBook());
/*  42:    */   }
/*  43:    */   
/*  44:    */   private void initBook(Book book)
/*  45:    */   {
/*  46: 45 */     if (book == null) {
/*  47: 46 */       return;
/*  48:    */     }
/*  49: 48 */     JTable table = new JTable(createTableData(book.getMetadata()), new String[] { "", "" });
/*  50:    */     
/*  51:    */ 
/*  52: 51 */     table.setEnabled(false);
/*  53: 52 */     table.setFillsViewportHeight(true);
/*  54: 53 */     JPanel contentPanel = new JPanel(new BorderLayout(0, 10));
/*  55: 54 */     contentPanel.add(table, "Center");
/*  56: 55 */     setCoverImage(contentPanel, book);
/*  57:    */     
/*  58: 57 */     this.scrollPane.getViewport().removeAll();
/*  59: 58 */     this.scrollPane.getViewport().add(contentPanel);
/*  60:    */   }
/*  61:    */   
/*  62:    */   private void setCoverImage(JPanel contentPanel, Book book)
/*  63:    */   {
/*  64: 62 */     if (book == null) {
/*  65: 63 */       return;
/*  66:    */     }
/*  67: 65 */     Resource coverImageResource = book.getCoverImage();
/*  68: 66 */     if (coverImageResource == null) {
/*  69: 67 */       return;
/*  70:    */     }
/*  71:    */     try
/*  72:    */     {
/*  73: 70 */       Image image = ImageIO.read(coverImageResource.getInputStream());
/*  74: 71 */       if (image == null)
/*  75:    */       {
/*  76: 72 */         log.error("Unable to load cover image from book");
/*  77: 73 */         return;
/*  78:    */       }
/*  79: 75 */       image = image.getScaledInstance(200, -1, 4);
/*  80: 76 */       JLabel label = new JLabel(new ImageIcon(image));
/*  81:    */       
/*  82: 78 */       contentPanel.add(label, "North");
/*  83:    */     }
/*  84:    */     catch (IOException e)
/*  85:    */     {
/*  86: 80 */       log.error("Unable to load cover image from book", e.getMessage());
/*  87:    */     }
/*  88:    */   }
/*  89:    */   
/*  90:    */   private Object[][] createTableData(Metadata metadata)
/*  91:    */   {
/*  92: 85 */     List<String[]> result = new ArrayList();
/*  93: 86 */     addStrings(metadata.getIdentifiers(), "Identifier", result);
/*  94: 87 */     addStrings(metadata.getTitles(), "Title", result);
/*  95: 88 */     addStrings(metadata.getAuthors(), "Author", result);
/*  96: 89 */     result.add(new String[] { "Language", metadata.getLanguage() });
/*  97: 90 */     addStrings(metadata.getContributors(), "Contributor", result);
/*  98: 91 */     addStrings(metadata.getDescriptions(), "Description", result);
/*  99: 92 */     addStrings(metadata.getPublishers(), "Publisher", result);
/* 100: 93 */     addStrings(metadata.getDates(), "Date", result);
/* 101: 94 */     addStrings(metadata.getSubjects(), "Subject", result);
/* 102: 95 */     addStrings(metadata.getTypes(), "Type", result);
/* 103: 96 */     addStrings(metadata.getRights(), "Rights", result);
/* 104: 97 */     result.add(new String[] { "Format", metadata.getFormat() });
/* 105: 98 */     return (Object[][])result.toArray(new Object[result.size()][2]);
/* 106:    */   }
/* 107:    */   
/* 108:    */   private void addStrings(List<? extends Object> values, String label, List<String[]> result)
/* 109:    */   {
/* 110:102 */     boolean labelWritten = false;
/* 111:103 */     for (int i = 0; i < values.size(); i++)
/* 112:    */     {
/* 113:104 */       Object value = values.get(i);
/* 114:105 */       if (value != null)
/* 115:    */       {
/* 116:108 */         String valueString = String.valueOf(value);
/* 117:109 */         if (!StringUtils.isBlank(valueString))
/* 118:    */         {
/* 119:113 */           String currentLabel = "";
/* 120:114 */           if (!labelWritten)
/* 121:    */           {
/* 122:115 */             currentLabel = label;
/* 123:116 */             labelWritten = true;
/* 124:    */           }
/* 125:118 */           result.add(new String[] { currentLabel, valueString });
/* 126:    */         }
/* 127:    */       }
/* 128:    */     }
/* 129:    */   }
/* 130:    */   
/* 131:    */   private TableModel createTableModel(Navigator navigator)
/* 132:    */   {
/* 133:124 */     new AbstractTableModel()
/* 134:    */     {
/* 135:    */       public Object getValueAt(int rowIndex, int columnIndex)
/* 136:    */       {
/* 137:129 */         return null;
/* 138:    */       }
/* 139:    */       
/* 140:    */       public int getRowCount()
/* 141:    */       {
/* 142:135 */         return 0;
/* 143:    */       }
/* 144:    */       
/* 145:    */       public int getColumnCount()
/* 146:    */       {
/* 147:140 */         return 2;
/* 148:    */       }
/* 149:    */     };
/* 150:    */   }
/* 151:    */   
/* 152:    */   public void navigationPerformed(NavigationEvent navigationEvent)
/* 153:    */   {
/* 154:147 */     if (navigationEvent.isBookChanged()) {
/* 155:148 */       initBook(navigationEvent.getCurrentBook());
/* 156:    */     }
/* 157:    */   }
/* 158:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.MetadataPane
 * JD-Core Version:    0.7.0.1
 */